package com.zzyl.service.impl;

import com.google.common.collect.Lists;
import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WeChatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

@Slf4j
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private WeChatService weChatService;
    
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;

    @Override
    public LoginVo login(UserLoginRequestDto ulrd) {
        //1. 获取 小程序 传递的code,发起 远程调用 获取openId
        String openId = weChatService.getOpenId(ulrd.getCode());

        //2. 根据openId 查询 数据库,查询用户
        Member member = memberMapper.getByOpenId(openId);

        //3. 用户为空,构建一个用户对象,赋值openId
        if (ObjectUtil.isEmpty(member)) {
            member = Member.builder().openId(openId).build();
        }

        //4. 根据detail.code 发起请求 获取真实的手机号
        String phone = weChatService.getPhone(ulrd.getPhoneCode());

        //5. 根据不同情况 新增/更新
        saveOrUpdate(member, phone);


        //6. 生成token 返回(包含用户ID 和 昵称)
        HashMap<String, Object> map = new HashMap<>();
        map.put(Constants.JWT_USERID,member.getId());
        map.put(Constants.JWT_USERNAME,member.getName());

        String key = jwtTokenManagerProperties.getBase64EncodedSecretKey();
        Integer millisecond = jwtTokenManagerProperties.getTtl();

        String jwt = JwtUtil.createJWT(key, millisecond, map);

        //7. 封装 LoginVo 对象 返回
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(jwt);
        loginVo.setNickName(member.getName());

        log.info("封装 LoginVo 对象:{}",loginVo);
        log.info("Member 对象:{}",member);
        return loginVo;
    }

    static ArrayList DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );

    private void saveOrUpdate(Member member, String phone) {
        //是否需要更新手机号
        if (!phone.equals(member.getPhone())) {
            member.setPhone(phone);
        }

        //是新增还是更新
        if (!ObjectUtil.isEmpty(member.getId())) {
            memberMapper.updateMember(member);
            return;
        }

        //新增
        //自动生成昵称 : 随机的美好祝愿 + 手机号后四位
        //先获取手机号后4位
        String phoneStr = phone.substring(7);

        //获取随机索引
        Random random = new Random();
        int index = random.nextInt(DEFAULT_NICKNAME_PREFIX.size());

        String nickName = DEFAULT_NICKNAME_PREFIX.get(index) + phoneStr;

        member.setName(nickName);

        memberMapper.addMember(member);
    }
}
